package cn.ymatrix.mxgateSDKRabbitMQTOYMatrix.rabbitmqOperate;

import cn.ymatrix.utils.Config;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class RabbitMQDataProducer {
  private static final String QUEUE_NAME = Config.getQueueName();

  private static final String QUEUE_HOST = Config.getQueueHost();
  private static final String QUEUE_USERNAME = Config.getQueueUserName();
  private static final String QUEUE_PASSWORD = Config.getQueuePassword();

  // 声明（如果不存在则创建）一个队列
  private static final boolean queueISexist = Config.isQueueISexist();

  public static void main(String[] args) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(QUEUE_HOST);
    factory.setUsername(QUEUE_USERNAME);
    factory.setPassword(QUEUE_PASSWORD);

    try (Connection connection = factory.newConnection();
        Channel channel = connection.createChannel()) {

      channel.queueDeclare(QUEUE_NAME, queueISexist, false, false, null);

      for (int i = 0; i < 1000000; i++) {
        // 准备要发送的消息
        String message =
            "{\"ts\":\"2022-12-29 15:34:31\",\"tag\":104,\"c1\":5200.87,\"c2\":2409.05,\"c3\":6576.82,\"c4\":6087.7,\"c5\":\"43f5ce75ddabea129e233049d31c8899\",\"c6\":\"19fe04378c1af99ec4691d00deba3476\",\"c7\":\"0dfa76a0c19ae74d2b90622b1de2f0f6\",\"c8\":\"3276d6057697690de120b6315eb8514b\"}\n";
        byte[] messageBodyBytes = message.getBytes();

        // 发送消息到队列
        channel.basicPublish("", QUEUE_NAME, null, messageBodyBytes);
        // System.out.println("Sent RabbitMQ Data:" + message);
      }
    }
  }
}
